﻿#region Copyright & License
//
// Copyright 2010 sd&c Hofmann 
// 
// mailto:info@sdch.de
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
#endregion

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint.Administration;
using System.Xml;
using Microsoft.Office.Server.Diagnostics;

namespace SDCH.SharePoint.Log4Sandbox
{
    public class SiteLogRepositoryConfigurationObject : SPPersistedObject
    {
        private const string c_strLastUpdateKey = "LastUpdateProperty";
        private const string c_strConfigurationKey = "RepositoryConfiguration";
        // TODO Change config read strategy
        //        private string xml = @"<?xml version=""1.0"" encoding=""utf-8"" ?>
        //                            <log4net> 
        //                                <appender name=""ULSLogAppender"" type=""SDCH.SharePoint.Log4Sandbox.LogAppender.ULSLogAppender, SDCH.SharePoint.Log4Sandbox, version=1.0.0.0, culture=neutral, PublicKeyToken=e416e6805e85b0ee"">
        //                              </appender>
        //                              <root>
        //                                <level value=""DEBUG"" />
        //                                <appender-ref ref=""ULSLogAppender"" />
        //                              </root>
        //                            </log4net>";


        #region constructor

        public SiteLogRepositoryConfigurationObject()
        {

        }

        public SiteLogRepositoryConfigurationObject(string pName, SPPersistedObject pParent)
            : base(pName, pParent)
        {

        }

        public SiteLogRepositoryConfigurationObject(string pName, SPPersistedObject pParent, Guid pID)
            : base(pName, pParent, pID)
        {

        }

        #endregion

        public override void Update()
        {
            _dtLastUpdate = DateTime.Now;
            base.Update();
        }

        // to short cut the permission check in method SPPersistedObject BaseUpdate()
        // the service account SPUCWorkerProcessProxy runs in must be Farm Administrator or at least write enabled at Farm Configuration DB
        protected override bool HasAdditionalUpdateAccess()
        {
            return true;
        }

        [Persisted]
        private DateTime _dtLastUpdate;

        public DateTime LastUpdate
        {
            get { return _dtLastUpdate; }
            private set { _dtLastUpdate = value; }
        }

        [Persisted]
        private string _strXmlConfig;

        public string XmlConfiguration
        {
            get { return _strXmlConfig; }
            set { _strXmlConfig = value; }
        }

        public XmlElement Configuration
        {
            get
            {
                try
                {
                    XmlDocument doc = new XmlDocument();
                    doc.LoadXml(_strXmlConfig);
                    return doc["log4net"];
                }
                catch (Exception ex)
                {
                    PortalLog.DebugLogString(PortalLogLevel.Unexpected, "[Unable to load log4Sandbox configuration SiteID:{0}] [{1}] {2}", this.Name, ex.Message, ex.StackTrace);
                    return null;
                }
            }
        }
        public bool IsCustomConfig
        {
            get { return !string.IsNullOrEmpty(_strXmlConfig); }
        }
    }
}
